package homework.day03;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Classname: HomeWork04
 * @Author: bromide
 * @CreateTime: 2022/4/2--13:37
 * @Version: V1.0
 * @Description: 提高题
 * 完成一个转码工具。
 * 比如当前项目目录下有一个文本文件note.txt，字符串编码
 * 是GBK的，现在需要将这个文件内容转换为UTF-8编码，并将其
 * 写出到文件note_utf.txt中。
 */
public class HomeWork04 {
    public static void main(String[] args) throws IOException {
        File file = new File("./src/homework/day03/note.txt");
        //先创建一个note.txt文件  以GBK编码格式写一句话
        /*FileOutputStream fos = new FileOutputStream(file,true);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        pw.println("大家都好啊！");*/

        //将note.txt的文件以GBK的形式读取出来并转成二进制
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        BufferedReader br = new BufferedReader(isr);

        //创建一个输出流将读出来的以utf-8的格式写进去
        File file1 = new File("./src/homework/day03/note_utf.txt");
        FileOutputStream fos = new FileOutputStream(file1);
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw, true);
        String line;
        while ((line = br.readLine()) != null) {
            pw.println(line);
        }
        System.out.println("转换完成");

    }
}
